import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { UserModule } from './module/user/user.module';
import { PrismaModule } from './module/prisma/prisma.module';
import { WinstonModule } from 'nest-winston';
import { jwtConfig, winstonConfig } from './config';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { HttpExceptionFilter, MultipartErrorFilter } from './common/filter';
import { FormatResponseInterceptor, InvokeRecordInterceptor, MultipartInterceptor } from './common/interceptor';
import { EmailModule } from './module/email/email.module';
import { JwtModule } from '@nestjs/jwt';
import { LoginGuard, PermissionGuard } from './common/guard';
import { MainlyCategoryModule } from './module/mainly-category/mainly-category.module';
import { SubCategoryModule } from './module/sub-category/sub-category.module';
import { ArticleModule } from './module/article/article.module';
import { TodoTaskModule } from './module/todo-task/todo-task.module';

@Module({
  imports: [
    // 配置项
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `.env.${process.env.NODE_ENV || 'development'}`,
    }),

    // 日志
    WinstonModule.forRootAsync(winstonConfig),

    // jwt
    JwtModule.registerAsync(jwtConfig),   

    // module
    UserModule,
    PrismaModule,
    EmailModule,
    MainlyCategoryModule,
    SubCategoryModule,
    ArticleModule,
    TodoTaskModule,
  ],

  controllers: [AppController],

  // provider
  providers: [
    AppService,
    // 多个 provider 使用相同的 token（如 APP_FILTER）时，NestJS 会将它们都注册为全局过滤器（或拦截器、守卫等），并按顺序依次执行。
    {
      provide: APP_FILTER,
      useClass: HttpExceptionFilter
    },
    {
      provide: APP_FILTER,
      useClass: MultipartErrorFilter
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: FormatResponseInterceptor
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: InvokeRecordInterceptor
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: MultipartInterceptor
    },
    {
      provide: APP_GUARD,
      useClass: LoginGuard
    },
    {
      provide: APP_GUARD,
      useClass: PermissionGuard
    }
  ],
})
export class AppModule { }
